﻿using System.Collections.Generic;
using System.Xml.Linq;
using System.IO;

namespace MVCCentral.Framework.Xml
{
    /// <summary>
    /// This is used with the Xml files used to populate Drop Down controls,
    /// like US States and Countries.
    /// </summary>
    public class SelectLoader
    {
        private XDocument _xml;

        #region ctors
        public SelectLoader(string xmlFilePath)
        {
            Load(xmlFilePath);
        }
        #endregion

        #region Public Properties

        #endregion

        #region Private Methods

        private void Load(string xmlFilePath)
        {
            _xml = XDocument.Load(xmlFilePath);
            if (_xml == null) throw new FileNotFoundException();
        }

        public List<SelectObject> GetList(string rootNodeName)
        {
            List<SelectObject> nodes = new List<SelectObject>();
            foreach (var item in _xml.Element(rootNodeName).Elements("item"))
            {
                SelectObject obj = new SelectObject();
                obj.Value = item.Attribute("Value").Value;
                obj.Text = item.Attribute("Text").Value;
                nodes.Add(obj);
            }

            return nodes;
        }
        #endregion
    }
}
